<?php 
class SysUser extends base_model
{
	//202cb962ac59075b964b07152d234b70 -->123
	var $uname;
	var $upassword;
	var $empid;
	var $firstname;
	var $midname;
	var $lastname;
	var $email;
	var $lastlogin;
	var $errorcount;
	var $islocked=0;
	var $changepassword=0;

	public function __construct($un='')
	{			
		$this->load->library('sys/sysconfig');
		if($un!='')
			$this->LookUp($un);
	}

	function Update()
	{		
		$this->lstupdateby=$this->loginuser['uname'];
		$this->lstupdate=date('Y-m-d H:i:s');		
		$this->db->where('uname', $this->uname);
		return $this->db->update($this->db->dbprefix('sysuser'), $this); 
	}

	function ChangePassword()
	{
		$this->lstupdateby=$this->loginuser['uname'];
		$this->lstupdate=date('Y-m-d H:i:s');		
		$this->changepassword=0;
		$this->db->where('uname', $this->uname);		
		return $this->db->update($this->db->dbprefix('sysuser'), $this); 
	}
	function IncErrorCount()
	{
		$cfg=0;
		$cfg=(SysConfig::GetConfigByKey('errorlocked'));
		$this->errorcount++;			
		if($this->errorcount >= $cfg->valueconfig){
			//print('masuk');
			$this->islocked=true;
		}
		
		$this->db->where('uname', $this->uname);
		return $this->db->update($this->db->dbprefix('sysuser'), $this); 
	}
	
	function Create($id)
	{	
		$this->createdby=$this->loginuser['uname'];
		$this->created=date('Y-m-d H:i:s');					
		return $this->db->insert($this->db->dbprefix('sysuser'), $this); 
	}

	function LookUp($uname)
	{
		$rw= $this->get_user($uname);
		if(count($rw)>0){				
			$this->uname=$rw['uname'];
			$this->upassword=$rw['upassword'];
			$this->empid=$rw['empid'];
			$this->firstname=$rw['firstname'];
			$this->midname=$rw['midname'];
			$this->lastname=$rw['lastname'];
			$this->email=$rw['email'];
			$this->lastlogin=$rw['lastlogin'];
			$this->errorcount=$rw['errorcount'];
			$this->islocked= ($rw['islocked']=='0'?0:$rw['islocked']);	
			$this->changepassword= ($rw['changepassword']=='0'?0:$rw['changepassword']);
			//$this->created=$rw['created'];
			//$this->createdby=$rw['createdby'];
			//$this->lstupdate=$rw['lstupdate'];
			//$this->lstupdateby=$rw['lstupdateby'];
			$this->parentmap($rw);
		}
		else{
			return false;
		}
	}

	function Delete($id)
	{
		$this->db->where_in('uname', $id);
		return $this->db->delete($this->db->dbprefix('sysuser')); 		
	}

	function LockUser()
	{
		$this->islocked=true;
		$this->db->where('uname', $this->uname);
		return $this->db->update($this->db->dbprefix('sysuser'), $this); 		
	}

	function verifyuserpassword($uid,$pass)
	{		
		$query = $this->db->get_where($this->db->dbprefix('sysuser'), array('uname' => $uid,'upassword'=>MD5($pass)));				
		$rslt= $query->row_array();
		
		if(count($rslt)>0){
			$this->errorcount=0;
			$this->lastlogin=date('Y-m-d H:i:s');	
			$this->db->where('uname', $this->uname);
			$this->db->update($this->db->dbprefix('sysuser'), $this); 			
			return $rslt;
			}
		else
			return false;
	}

	public function get_user($slug = FALSE)
	{	
		if ($slug === FALSE)
		{			
			$query = $this->db->get($this->db->dbprefix('sysuser'));		
			return $query->result_array();
		}
		$query = $this->db->get_where($this->db->dbprefix('sysuser'), array('uname' => $slug));
		return $query->row_array();
	} 
}



?>



